﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Model;

namespace Control
{
    public class MGTabelas : ModuloDeGerenciamento<Tabela>, IEditavel<Tabela>, IDuplicavel<Tabela>, INegociavel<Tabela, FonteDeDado>
    {
        public MGTabelas()
        {
            this.dc = new Model.DataClassesDataContext();
        }

        public override List<Tabela> listar()
        {
            var tabela = from row in this.dc.Tabelas select row;
            List<Tabela> list = new List<Tabela>();
            foreach (Tabela t in tabela)
            {
                list.Add(t);
            }
            return list;
        }

        public override bool adicionar(Tabela obj)
        {
            bool flag = true;
            try
            {
                this.dc.Tabelas.InsertOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {

                this.output += "Erro ao inserir o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }

        public bool adicionar(string nome, string descricao)
        {
            if (string.IsNullOrEmpty(nome) || string.IsNullOrEmpty(descricao))
            {
                return false;
            }
            Tabela t = new Tabela();
            t.nome = nome;
            t.descricao = descricao;
            return this.adicionar(t);
        }

        public override bool remover(Tabela obj)
        {
            var flag = false;
            try
            {
                this.dc.Tabelas.DeleteOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                flag = true;
            }
            return flag;
        }

        public override Tabela get(uint id)
        {
            if (id == 0) return null;
            Tabela tabela = null;
            try
            {
                tabela = this.dc.Tabelas.Single(t => t.id == id);
            }
            catch (Exception e)
            {

                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                tabela = null;
            }
            return tabela;
        }
        public override bool remover(uint id)
        {
            return this.remover(this.get(id));
        }

		public bool atualizar(Tabela obj) {
			throw new NotImplementedException();
		}
		
		public bool atualizar(uint id, string nome, string descricao) {
			throw new NotImplementedException();
		}

		public Tabela duplicar(Tabela obj) {
			throw new NotImplementedException();
		}

		public Tabela duplicar(uint id) {
			throw new NotImplementedException();
		}

		public bool negociar(Tabela produto, FonteDeDado exportador, FonteDeDado importador) {
			throw new NotImplementedException();
		}

		public bool negociar(uint produto, uint exportador, uint importador) {
			throw new NotImplementedException();
		}
	}
}
